package com.mbyte.easy.rest.draft;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mbyte.easy.admin.entity.Draft;
import com.mbyte.easy.admin.service.IDraftService;
import com.mbyte.easy.common.controller.BaseController;
import com.mbyte.easy.common.web.AjaxResult;
import com.mbyte.easy.event.CmsEventPublisher;
import com.mbyte.easy.support.es.constants.EsConstants;
import com.mbyte.easy.support.es.helper.EsDraftHelper;
import com.mbyte.easy.util.PageInfo;
import com.sun.media.jfxmedia.logging.Logger;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
* <p>
* 前端控制器
* </p>
* @author lp
* @since 2019-03-11
*/
@Slf4j
@RestController
@RequestMapping("rest/draft")
public class RestDraftController extends BaseController  {

    @Autowired
    private IDraftService draftService;
    @Autowired
    private CmsEventPublisher cmsEventPublisher;


    /**
    * 查询列表
    *
    * @param pageNo
    * @param pageSize
    * @param draft
    * @return
    */
    @RequestMapping
    public AjaxResult index(@RequestParam(value = "pageNo", required = false, defaultValue = "1") Integer pageNo,@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize, String submitTimeSpace, String createTimeSpace, String updateTimeSpace, Draft draft) {
        Page<Draft> page = new Page<Draft>(pageNo, pageSize);
        QueryWrapper<Draft> queryWrapper = new QueryWrapper<Draft>();

        if(draft.getDraftTitle() != null  && !"".equals(draft.getDraftTitle() + "")) {
            queryWrapper = queryWrapper.like("draft_title",draft.getDraftTitle());
         }


        if(draft.getDraftText() != null  && !"".equals(draft.getDraftText() + "")) {
            queryWrapper = queryWrapper.like("draft_text",draft.getDraftText());
         }


        if(draft.getDraftNum() != null  && !"".equals(draft.getDraftNum() + "")) {
            queryWrapper = queryWrapper.like("draft_num",draft.getDraftNum());
         }


        if(draft.getDraftType() != null  && !"".equals(draft.getDraftType() + "")) {
            queryWrapper = queryWrapper.like("draft_type",draft.getDraftType());
         }


        if(draft.getSubmitTime() != null  && !"".equals(draft.getSubmitTime() + "")) {
            queryWrapper = queryWrapper.like("submit_time",draft.getSubmitTime());
         }


        if(draft.getUserId() != null  && !"".equals(draft.getUserId() + "")) {
            queryWrapper = queryWrapper.like("user_id",draft.getUserId());
         }


        if(draft.getStatusCode() != null  && !"".equals(draft.getStatusCode() + "")) {
            queryWrapper = queryWrapper.like("status_code",draft.getStatusCode());
         }


        if(draft.getIsDelete() != null  && !"".equals(draft.getIsDelete() + "")) {
            queryWrapper = queryWrapper.like("is_delete",draft.getIsDelete());
         }


        if(draft.getGroupId() != null  && !"".equals(draft.getGroupId() + "")) {
            queryWrapper = queryWrapper.like("group_id",draft.getGroupId());
         }


        if(draft.getLastOperator() != null  && !"".equals(draft.getLastOperator() + "")) {
            queryWrapper = queryWrapper.like("last_operator",draft.getLastOperator());
         }


        if(draft.getIsPass() != null  && !"".equals(draft.getIsPass() + "")) {
            queryWrapper = queryWrapper.like("is_pass",draft.getIsPass());
         }

        IPage<Draft> pageInfo = draftService.page(page, queryWrapper);

        Map<String, Object> map = new HashMap<>();
        map.put("searchInfo",  draft);
        map.put("pageInfo", new PageInfo(pageInfo));
        return this.success(map);
    }

    @RequestMapping("testSync")
    public AjaxResult testSync(){
        Draft draft = new Draft();
        draft.setDraftTitle("lxt测试");
        draft.setDraftText("英国考古前遗迹巨石阵900米处，发现了另外一个环形阵。这个直径大约25" +
                "米左右的环状沟渠里面有一些日本民众赶往东京小石川植物园，一睹巨型“泰坦魔芋" +
                "花”的风采。这株高1.5米的“泰坦魔芋花”上一次开放还是在1991年。“泰坦魔芋花” 原产自印度尼西亚苏门达腊的热带雨林地区，是世界上最大的花，也是气味最臭" +
                "的花，一般十六年左右才开一次花，而且花期很短，每次只开放几天。\n" +
                "\n");
        draftService.insert2Id(draft);
        Draft draft1 = new Draft();
        draft1.setDraftTitle("lxt测试");
        draft1.setDraftText("英国考古前遗迹巨.5米的“泰坦魔芋花”.5米的“泰坦魔芋花”.5米的“泰坦魔芋花”石阵900米处，发现了另外一个环形阵。这个直径大约25" +
                "米左右的环状沟渠里面有一些日本民众赶往东京小石川植物园，一睹巨型“泰坦魔芋" +
                "花”的风采。这株高1.5米的“泰坦魔芋花”上一次开放还是在1991年。“泰坦魔芋花” 原产自印度尼西亚苏门达腊的热带雨林地区，是世界上最大的花，也是气味最臭" +
                "的花，一般十六年左右才开一次花，而且花期很短.5米的“泰坦魔芋花”.5米的“泰坦魔芋花”.5米的“泰坦魔芋花”，每次只开放几天。\n" +
                "\n");
        draftService.insert2Id(draft1);
        Draft draft2 = new Draft();
        draft2.setDraftTitle("lxt测试");
        draft2.setDraftText("英国考古前遗迹巨石阵900米处，发现了另外一个环形阵。这个直径大约25" +
                "米左右的环状沟渠里面有一些日本民众赶往东京小石川植物园，一睹巨型“泰坦魔芋" +
                "花”的风采。这株高1.5米的“泰坦魔芋花”上一次开放还是在1991年。“泰坦魔芋花” 原产自印度尼西亚苏门达腊的热带雨林地区，是世界上最大的花，也是气味最臭" +
                "的花，一般十六年左右才开一次花，而且花一些日本民众赶往东京小石川一些日本民众赶往东京小石川一些日本民众赶往东京小石川期很短，每次只开放几天。\n" +
                "\n");
        draftService.insert2Id(draft2);
        cmsEventPublisher.publishChangeDraftEvent(draft
                .setEsSyncType(EsConstants.ES_SYNC_TYPE_ADD));
        cmsEventPublisher.publishChangeDraftEvent(draft1
                .setEsSyncType(EsConstants.ES_SYNC_TYPE_ADD));
        cmsEventPublisher.publishChangeDraftEvent(draft2
                .setEsSyncType(EsConstants.ES_SYNC_TYPE_ADD));
        return success();
    }

    /**
    * 添加
    * @param draft
    * @return
    */
    @PostMapping("add")
    public AjaxResult add(Draft draft){
        return toAjax(draftService.save(draft));
    }

    /**
    * 添加
    * @param draft
    * @return
    */
    @PostMapping("edit")
    public AjaxResult edit(Draft draft){
        return toAjax(draftService.updateById(draft));
    }
    /**
    * 删除
    * @param id
    * @return
    */
    @GetMapping("delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        return toAjax(draftService.removeById(id));
    }
    /**
    * 批量删除
    * @param ids
    * @return
    */
    @PostMapping("deleteAll")
    public AjaxResult deleteAll(@RequestBody List<Long> ids){
        return toAjax(draftService.removeByIds(ids));
    }
}

